$(function () {
    //大的表格
    var schedule_datagrid = $("#schedule_datagrid");
    //班级表格
    var cg_datagrid = $("#cg_datagrid");
    //日期
    var calendar = $("#calendar");
    schedule_datagrid.datagrid({
        url: "/schedule/list",
        columns: [[
            {field: 'subDate', title: '日期', width: 100},
            {field: 'weekday', title: '星期', width: 100},
            {field: 'classgrade', title: '班级', width: 100,formatter:formatterClassgrade},
            {field: 'subject', title: '课程名称', width: 100},
            //{field: 'employee', title: '班主任', width: 100,formatter:formatterEmployee},
            {field: 'employee', title: '上课教师', width: 100,formatter:formatterEmployee},
            {field: 'classroom', title: '教室', width: 100,formatter:formatterClassroom},
            {field: 'remark', title: '备注', width: 100},
            {field: 'status', title: '状态', width: 100,formatter:formatterStatus}
        ]],
        fit: true,
        fitColumns: true,
        border: false,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        toolbar: "#schedule_toolbar"
    });
    //把方法绑定到一个对象上
    var cmdObj = {
        //点击全部,将所有的选择的都清空,然后查询出来
        reload: function () {
            $("#btn_classgrade").combobox("clear");
            $("#btn_classroom").combobox("clear");
            $("#btn_teacher").combobox("clear");
            $("#btn_beginDate").datebox("clear");
            $("#btn_endDate").datebox("clear");

            $.post("/schedule/list", function (data) {
                schedule_datagrid.datagrid("load", data);
            });
            //schedule_datagrid.datagrid("reload");
        },//高级查询
        searchForSchedule: function () {
            var classgradeId = $("#btn_classgrade").val();
            var classroomId = $("#btn_classroom").val();
            var employeeId = $("#btn_teacher").val();
            var beginDate = $("#btn_beginDate").val();
            var endDate = $("#btn_endDate").val();
            //获取到每个选择框的id,去重新查询加载
            schedule_datagrid.datagrid("load", {
                classgradeId: classgradeId,
                classroomId: classroomId,
                employeeId: employeeId,
                beginDate: beginDate,
                endDate: endDate
            });
        }
    }
    //左下角班级显示
    cg_datagrid.datagrid({
        url: "/classgrade/list",
        columns: [[
            {field: 'name', width: 100},
        ]],
        fit: true,
        fitColumns: true,
        border: false,
        //rownumbers: true,
        singleSelect: true,
        onDblClickRow:function (index, row) {
            var classgradeId = row.id;
            schedule_datagrid.datagrid("load", {
                classgradeId: classgradeId
            });
        }
    });
    //用户选择日期时触发
    calendar.calendar({
        onSelect: function(date){
            var data = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
            schedule_datagrid.datagrid("load", {
                beginDate: data,
                endDate: data
            });
        }
    });
    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});
//格式化状态
function formatterStatus(value,row,index) {
    if(value == 0){
        return "<font color='green'>已上</font>";
    }
    if(value == 1){
        return "<font color='red'>未上</font>";
    }
}
//格式化教室
function formatterClassroom(value,row,index) {
    return value ? value.name : "";
}
//格式化上课老师
function formatterEmployee(value,row,index) {
    return value ? value.realName : "";
}
//格式化班级
function formatterClassgrade(value,row,index) {
    return value ? value.name : "";
}
